package com.youan.serve.controller;

import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.RoleControllerSwagger;
import com.youan.serve.dao.domain.Role;
import com.youan.serve.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/role")
public class RoleController implements RoleControllerSwagger{
    @Autowired
    private IRoleService roleService;

    @Override
    @PostMapping("/role")
    public Role addRole(Role role, LoginInfor loginInfor) throws Exception{
        if(roleService.addJudgeRole(role)){
            role.setRoleId(UUID.randomUUID().toString().replaceAll("-", ""));
            role.setOperateTime(new Date());
            role.setOperator(loginInfor.getUsername());
            roleService.save(role);
        }
        return null;
    }

    @Override
    @PutMapping("/role")
    public Role updateRole(Role role, LoginInfor loginInfor){
        role.setOperator(loginInfor.getUsername());
        role.setOperateTime(new Date());
        roleService.updateById(role);
        return null;
    }

    @Override
    @GetMapping("/roles")
    public List<Role> selectAllRole(){
        return roleService.list();
    }

    @Override
    @DeleteMapping("/role/{roleId}")
    public void deleteRoleByRoleId(@PathVariable String roleId) throws Exception{
        if(roleService.deleteJudgeRole(roleId)){
            roleService.removeById(roleId);
        }
    }

    @Override
    @GetMapping("/role/{roleId}")
    public Role selectRoleByRoleId(@PathVariable String roleId){
        return roleService.getById(roleId);
    }
}

